home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c
- Subject: Re: Help with gettng 2 chars into an integer!
- Date: 11 Jan 1996 15:01:36 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan11100137@g7240065.bridge.bst.bls.com>
- References: <4d2eh1$7pm@usc.edu>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: wawda@scf.usc.edu's message of 11 Jan 1996 07:33:53 GMT
-
- In article <4d2eh1$7pm@usc.edu> wawda@scf.usc.edu (Abu Wawda) writes:
-
- : Hi. I can't seem to figure out how to do this. Basically I have to chars that
- : I want to put into a 2-byte integer. It seems easy at first, but I can't seem
- : to figure out to do it with the bit-fidling operators (shifting or masking).
- : Simple put, I'd like to do:
-
- : char a,b;
- : int c;
-
- : a = 'A';
- : b = 'B';
- : c = ? /* the first byte in c should contain the value of a, and the
- : second byte should contain the value of b */
-
- A couple of solutions
-
- With bit shifting (assumes 8 bit bytes):
-
- c = (a << 8) | b;
-
- With a union (assumes ???? endianess):
-
- union {
- char a[2];
- short c;
- } fred;
-
- fred.a[1] = 'A';
- fred.a[2] = 'B';
-
- And thats it.
-
- Hope this helps
- Regards
-
- -A.
- --
- | A.Champion |
-